#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'


from PySide import QtGui, QtCore
import os

from . import ui_style

class TaskPage(QtGui.QDialog):
    
    def __init__(self, parent=None):
        super(TaskPage, self).__init__(parent=parent)
        self.setStyleSheet(ui_style.STANDARD_STYLE)
        # >> server task
        main_layout = QtGui.QVBoxLayout()
        main_layout.setContentsMargins(0,0,0,0)
        self.task_view = QtGui.QTableView()
        self.task_view.setFocusPolicy(QtCore.Qt.NoFocus)
        self.task_view.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.task_view.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.task_view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.view_model = QtGui.QStandardItemModel()
        self.task_view.setModel(self.view_model)
        self.selection_model = self.task_view.selectionModel()
        main_layout.addWidget(self.task_view)
        self.setLayout(main_layout)
        
        self._header_item()
        
    def _header_item(self):
        self.view_model.setHorizontalHeaderItem(0, QtGui.QStandardItem(u"文件名"))
        self.view_model.setHorizontalHeaderItem(1, QtGui.QStandardItem(u"类型"))
        self.view_model.setHorizontalHeaderItem(2, QtGui.QStandardItem(u"任务分组"))
        self.view_model.setHorizontalHeaderItem(3, QtGui.QStandardItem(u"所有者"))
        self.view_model.setHorizontalHeaderItem(4, QtGui.QStandardItem(u"状态"))
        
    def add_items(self, task_data, type_data, *args, **kwargs):
        if not task_data:
            return
        if isinstance(task_data, dict):
            file_list = task_data.keys()
            file_list.sort()
            for task_name in file_list:
                self.add_item(type_data, task_name, task_data[task_name]['status'], task_data[task_name]['image'], user=task_data[task_name]['user'], *args, **kwargs)
     
    def add_item(self, type_data, task_name, status, image, *args, **kwargs):
        user = kwargs.get('user', None)
        manager = kwargs.get('manager', None)
        
        if not user:
            if not manager:
                return
            else:
                user = manager.id
        row = self.view_model.rowCount()
        self.view_model.setItem(row, 0, QtGui.QStandardItem(QtGui.QIcon(image),task_name))
        self.view_model.setItem(row, 1, QtGui.QStandardItem(type_data['type']))
        self.view_model.setItem(row, 2, QtGui.QStandardItem(type_data['sub']))
        self.view_model.setItem(row, 3, QtGui.QStandardItem(user))
        self.view_model.setItem(row, 4, QtGui.QStandardItem(status))
        if manager:
            if status == 'Waiting':
                cmd_str = self._parent.get_cmd(manager, type_data, task_name)
                item = self.view_model.item(row)
                # print cmd_str
                self._parent.TaskThread.task_data.append({'cmd':cmd_str, "itemModel":self.view_model, 'item':item, 'manager':manager, 'file':task_name, 'type_data': type_data})

if __name__ == "__main__":
    app = QtGui.QApplication([])
    bmui = TaskPage()
    bmui.show()
    app.exec_()
        